package com.stylefeng.guns.rest.modular.wz.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.stylefeng.guns.rest.common.constant.Consts;
import com.stylefeng.guns.rest.common.persistence.dao.*;
import com.stylefeng.guns.rest.common.persistence.model.*;
import com.stylefeng.guns.rest.enums.EnWzAdv;
import com.stylefeng.guns.rest.enums.EnWzAdvMap;
import com.stylefeng.guns.rest.enums.EnWzUser;
import com.stylefeng.guns.rest.enums.EnWzUserFinanceRecord;
import com.stylefeng.guns.rest.modular.wz.controller.WzFinanceService;
import com.stylefeng.guns.rest.modular.wz.dao.WzAdminDao;
import com.stylefeng.guns.rest.modular.wz.dao.WzAdvDao;
import com.stylefeng.guns.rest.modular.wz.service.IPfFileService;
import com.stylefeng.guns.rest.modular.wz.util.AliOssUtil;
import com.stylefeng.guns.rest.modular.wz.util.wz.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 审核业务类
 */
@Service
public class WzAdminServiceImpl {

    @Autowired
    private WzAdvMapper advMapper;
    @Autowired
    private WzUserFinanceRecordMapper userFinanceRecordMapper;
    @Autowired
    private WzUserAdvServiceImpl userAdvService;
    @Autowired
    private WzUserServiceImpl userService;
    @Autowired
    private WzAdminDao adminDao;
    @Autowired
    private WzAdvMapMapper advMapMapper;
    @Autowired
    private WzUserMapper userMapper;
    @Autowired
    private PfFileMapper pfFileMapper;
    @Autowired
    private WzFinanceService financeService;
    @Autowired
    private WzMapRebateMapper mapRebateMapper;
    @Autowired
    private WzMapMergeMapper mapMergeMapper;

    public boolean saveAdvMapCheckSuccess(WzAdvMap advMap, WzUser user) throws Exception {
        if(advMap.getStatus() == EnWzAdvMap.STATUS_RelesseEditors.getIndex()){//修改审核中
            WzAdvMap newAdvMap = advMapMapper.selectById(advMap.getTargetId());
            advMap.setStatus(EnWzAdvMap.STATUS_Release.getIndex());
            advMap.setRejectRemark("");
            advMap.setMerchantFullAddress(newAdvMap.getMerchantFullAddress());
            advMap.setMerchantTel(newAdvMap.getMerchantTel());
            advMap.setAdvMapDetail(newAdvMap.getAdvMapDetail());
            advMap.setAvatarUrl(newAdvMap.getAvatarUrl());
            advMap.setLogo(newAdvMap.getLogo());
            advMap.setLatitude(newAdvMap.getLatitude());
            advMap.setLongitude(newAdvMap.getLongitude());
            advMap.setName(newAdvMap.getName());
            advMap.setSubtitle(newAdvMap.getSubtitle());
            advMap.setShareLink(newAdvMap.getShareLink());
            advMap.setUserInfoId(newAdvMap.getUserInfoId());
            //修改后文件
            List<PfFile> newPfFiles = pfFileMapper.selectList(new EntityWrapper<PfFile>().eq("target_id",advMap.getTargetId()));
            advMap.setTargetId("");
            //数据保存
            boolean r = adminDao.saveAdvMapCheckSuccessRelesseEditors(newPfFiles,advMap,newAdvMap);
            return r;
        }
        if(advMap.getStatus() == EnWzAdvMap.STATUS_Checking.getIndex()){//新增审核中
            if(advMap.getPublishingType() == EnWzAdvMap.Publishing_Type_User.getIndex()){//用户发布
                if(advMap.getSubtype() == EnWzAdvMap.SUBTYPE_Adv.getIndex()){//基本广告
                    //地图广告审核通过
                    String date = DateUtil.toString(DateUtil.getDateAfter(new Date(), advMap.getDayNumber())) + " 23:59:59";
                    advMap.setEndTime(DateUtil.toDateTime(date));
                    advMap.setDayNumber(0);
                    advMap.setStatus(EnWzAdvMap.STATUS_Release.getIndex());
                    //数据保存
                    boolean r = adminDao.saveAdvMapCheckSuccess(user,advMap);
                    if(r){
                        return true;
                    }
                }else if(advMap.getSubtype() == EnWzAdvMap.SUBTYPE_Redpacket_Adv.getIndex()){//带红包
                    String date = DateUtil.toString(DateUtil.getDateAfter(new Date(), advMap.getDayNumber())) + " 23:59:59";
                    advMap.setEndTime(DateUtil.toDateTime(date));
                    advMap.setDayNumber(0);
                    advMap.setStatus(EnWzAdvMap.STATUS_Release.getIndex());
                    //保存广告数据
                    WzAdv adv = new WzAdv();
                    Long advId = IdWorker.getId();
                    advMap.setRedpacketAdvId(advId.toString());
                    adv.setAdvId(advId);
                    adv.setNickName(user.getNickName());
                    adv.setAvatarUrl(user.getAvatarUrl());
                    adv.setScreenCity(advMap.getAdCode());
                    adv.setCoverUrl(advMap.getAvatarUrl());
                    adv.setTitle(advMap.getName());
                    adv.setSubtitle(advMap.getSubtitle());
                    adv.setAdvDetail(advMap.getAdvMapDetail());
                    adv.setUserCode(advMap.getUserCode());
                    adv.setUserInfoId(advMap.getUserInfoId());
                    adv.setShareLink(advMap.getShareLink());
                    adv.setLink(advMap.getLink());
                    adv.setLinkName(advMap.getLinkName());
                    adv.setCreateTime(new Date());
                    adv.setAdvMapId(advMap.getAdvMapId().toString());
                    adv.setStatus(EnWzAdv.STATUS_RELEASE_no.getIndex());//状态
                    adv.setSubtype(EnWzAdv.SUBTYPE_Redpacket_MapAdv.getIndex());//广告类型
                    adv.setPublishingType(EnWzUserFinanceRecord.Publishing_type_user.getIndex());//发布类型：用户发布
                    adv.setSubtitle(advMap.getSubtitle());
                    adv.setRewardAverage(advMap.getRedpacketAverageReward());//人均赏金金额
                    adv.setTotalNumber(advMap.getRedpacketTotalNumber());
                    int expense = advMap.getRedpacketAverageReward()*advMap.getRedpacketTotalNumber();//发布总金额
                    adv.setExpense(expense);
                    adv.setEndTime(advMap.getEndTime());
                    int serviceCharge = new BigDecimal(expense).multiply(new BigDecimal(Consts.redPacketFee)).setScale(0, BigDecimal.ROUND_UP).intValue();//广告服务费
                    int totalReward = expense - serviceCharge;//赏金总额
                    adv.setTotalReward(totalReward);//赏金总额
                    adv.setSurplusReward(totalReward);//剩余赏金
                    adv.setReleaseTime(new Date());//发布时间
                    String start = DateUtil.toString(new Date())+ " 00:00:00";
                    adv.setBeginTime(DateUtil.toDateTime(start));
                    adv.setEndTime(advMap.getEndTime());
                    //生成红包数据
                    userAdvService.delete(new EntityWrapper<WzUserAdv>().eq("adv_id",adv.getAdvId()));
                    List<WzUserAdv> userAdvs = userService.userAdvs(adv);
                    //数据保存
                    boolean r = adminDao.saveRedpackAdvMapCheckSuccess(userAdvs,user,advMap,adv);
                    if(r){
                        return true;
                    }
                }
            }else if(advMap.getPublishingType() == EnWzAdvMap.Publishing_Type_Admin.getIndex()){//后台发布
                if(advMap.getType() == EnWzAdvMap.TYPE_Adv.getIndex()){
                    String date = DateUtil.toString(DateUtil.getDateAfter(new Date(), advMap.getDayNumber())) + " 23:59:59";
                    advMap.setEndTime(DateUtil.toDateTime(date));
                    advMap.setStatus(EnWzAdvMap.STATUS_Pass.getIndex());
                    int r = advMapMapper.updateById(advMap);
                    if(r > 0){
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public boolean saveAdvMapCheckFail(WzAdvMap advMap,WzUser user) {
        if(advMap.getStatus() == EnWzAdvMap.STATUS_RelesseEditors.getIndex()){//修改审核中
            advMap.setStatus(EnWzAdvMap.STATUS_Edit_Reject.getIndex());
            int r = advMapMapper.updateById(advMap);
            if(r > 0){
                return true;
            }
        }
        if(advMap.getStatus() == EnWzAdvMap.STATUS_Checking.getIndex()){//新增审核中
            if(advMap.getPublishingType() == EnWzAdvMap.Publishing_Type_User.getIndex()) {//用户发布
                advMap.setStatus(EnWzAdvMap.STATUS_Add_Reject.getIndex());
                int amount = 0;
                if(advMap.getSubtype() == EnWzAdvMap.SUBTYPE_Adv.getIndex()){
                    amount = advMap.getDailyAmount()*advMap.getDayNumber();
                }else if(advMap.getSubtype() == EnWzAdvMap.SUBTYPE_Redpacket_Adv.getIndex()){
                    amount = advMap.getRedpacketAverageReward()*advMap.getRedpacketTotalNumber();
                }
                //数据保存
                boolean r = financeService.saveAdvMapCheckFail(user,advMap,amount);
                return r;
            }else if(advMap.getPublishingType() == EnWzAdvMap.Publishing_Type_Admin.getIndex()){//后台发布
                advMap.setStatus(EnWzAdvMap.STATUS_Add_Reject.getIndex());
                int r = advMapMapper.updateById(advMap);
                if(r > 0){
                    return true;
                }
            }
        }
        return false;
    }

    public boolean mapMergeCheck(WzAdvMap advMap, WzMapMerge mapMerge) {
        boolean r = adminDao.mapMergeCheck(advMap,mapMerge);
        return r;
    }

    public boolean mapRebateCheck(WzAdvMap advMap, WzMapRebate mapRebate) {
        boolean r = adminDao.mapRebateCheck(advMap,mapRebate);
        return r;
    }
}
